package demo2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int len = scanner.nextInt();
        // String str = scanner.nextLine();
        // String[] arr = str.split(" ");
        // String str1 = arr[0];
        // String str2 = arr[1];
        String str1 = scanner.next();
        String str2 = scanner.next();
        String[] strs = new String[len];
        for (int i = 0; i < len; i++) {
            strs[i] = scanner.next();
        }

        if (str1 == null || str2 == null) {
            System.out.println("-1");
        }

        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();

        for (int i = 0; i < len; i++) {
            if (strs[i].equals(str1)) {
                list1.add(i);
            }
            if (strs[i].equals(str2)) {
                list2.add(i);
            }
        }

        if (list1.isEmpty() || list2.isEmpty()) {
            System.out.println("-1");
            return;
        }

        int min = Math.abs(list1.get(0) - list2.get(0));
        int i = 0;
        int j = 0;
        while (i < list1.size() && j < list2.size()) {
            if (list1.get(i) < list2.get(j)) {
                int k = list2.get(j) - list1.get(i);
                if (min > k) {
                    min = k;
                }
                i++;
            } else {
                int k = list1.get(i) - list2.get(j);
                if (min > k) {
                    min = k;
                }
                j++;
            }
        }
        System.out.println(min);
    }


    public static void main5(String[] args) {
        Scanner in = new Scanner(System.in);
        Float a = in.nextFloat();
        String s = in.next();
        char b = s.charAt(0);

        int price = 20;
        if((a-1) > 0) {
            int c = a.intValue();
            if(a - c > 0) {
                c += 1;
            }
            price = price + (c-1)*1;
        }
        if(b == 'y') {
            price += 5;
        }
        System.out.println(price);

        List<Integer> list = new ArrayList<>();
        list.add(1);
    }
}
